import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  // 选中的用户
  const selectedUsers = ref([])
  
  // 当前正在编辑的用户
  const currentUser = ref(null)
  
  // 设置选中的用户
  const setSelectedUsers = (users) => {
    selectedUsers.value = [...users]
  }
  
  // 清空选中的用户
  const clearSelection = () => {
    selectedUsers.value = []
  }
  
  // 设置当前用户
  const setCurrentUser = (user) => {
    currentUser.value = user ? { ...user } : null
  }
  
  // 清空当前用户
  const clearCurrentUser = () => {
    currentUser.value = null
  }
  
  // 取消选择，保留上一次的选择
  const cancelSelection = () => {
    const oldSelection = [...selectedUsers.value]
    clearSelection()
    return oldSelection
  }
  
  return {
    selectedUsers,
    currentUser,
    setSelectedUsers,
    clearSelection,
    setCurrentUser,
    clearCurrentUser,
    cancelSelection
  }
}) 